<?php
class Db
{
    private $link;
    public function __construct($dbname,$user,$pwd,$host='127.0.0.1')
    {
        $this->link = new PDO("mysql:host=$host;dbname=$dbname", "$user", "$pwd");
        $this->link->setAttribute(PDO::ATTR_AUTOCOMMIT,true);
    }

    /**
     * 查询一条
     * @param $table
     * @param string $where
     * @return mixed|string
     */
    public function getOne($table,$where="1")
    {
        if(empty($table))
        {
            return "没有数据表";
            die;
        }
        if(empty($where))
        {
            return "没有查询条件";
            die;
        }
        $sql = "select * from `$table`";
        $res = $this->link->query($sql);
        return $res->fetch(2);
    }

    /**
     * 查询多条
     * @param $table
     * @param string $where
     * @return array|string
     */
    public function getAll($table,$where="1")
    {
        if(empty($table))
        {
            return "没有数据表";
            die;
        }
        if(empty($where))
        {
            return "没有查询条件";
            die;
        }
        $sql = "select * from `$table`";
        $res = $this->link->query($sql);
        return $res->fetchAll(2);
    }

    public function add($table,$array)
    {
        if(empty($table))
        {
            return "没有数据表";
            die;
        }
        if(count($array)==0)
        {
            return "没有添加的数据";
            die;
        }
        $filed = "";
        $val = "";
        foreach($array as $key=>$value)
        {
            $filed.="`$key`,";
            $val.="'$value',";
        }
        $filed = substr($filed,0,-1);
        $val = substr($val,0,-1);
        $sql = "insert into  `$table`($filed) value($val)";
        return $this->link->exec($sql);
    }
}



?>